/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProyectoBienestarUniversitarioComfenalcoCulturalis.src.java.mvc.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import mvc.modelos.Rol;

/**
 *
 * @author pezcado
 */
public class RolDAO implements IDAO<Rol>  {
    
    private PreparedStatement consultar;
    private PreparedStatement insertar;
    private PreparedStatement modificar;
    private PreparedStatement eliminar;
    private PreparedStatement listar;
    private Model model;
    
    public Rol consultar(int personaIdentificacion) throws SQLException {
        model= Model.getInstance();
        
        String sql="select persona_identificacion, tipoRol, id, estado, descripcion "
                + "from Rol where persona_identificacion="+personaIdentificacion;
        
        consultar=model.getCon().prepareStatement(sql);
        ResultSet resultSet= consultar.executeQuery();
        Rol rol= new Rol();
        
        try{
            resultSet.next();
                
            rol.setPersonaIdentificacion(resultSet.getInt("persona_identificacion"));
            rol.setTipoRol(resultSet.getString("tipoRol"));
            rol.setId(resultSet.getInt("id"));
            rol.setEstado(resultSet.getString("estado"));
            rol.setDescripcion(resultSet.getString("descripcion"));
            
            //return rol;
        }catch(Exception e){
        
        }
        return rol;
    }

    public void insertar(Rol IDAO) throws SQLException {
        model= Model.getInstance();
        
        if(insertar==null){
            String sql="insert into Rol(persona_identificacion, tipoRol, id, estado, descripcion) values(?,?,?,?,?)";
            insertar=model.getCon().prepareStatement(sql);
        }
        insertar.setInt(1, IDAO.getPersonaIdentificacion());
        insertar.setString(2, IDAO.getTipoRol());
        insertar.setString(3, null);
        insertar.setString(4, IDAO.getEstado());
        insertar.setString(5, IDAO.getDescripcion());
        
        insertar.executeUpdate();
    }

    public void modificar(Rol IDAO) throws SQLException {
        model= Model.getInstance();
        
        String sql="update from Rol set"
                + "tipoRol='"+IDAO.getTipoRol()+"',"
                //+ "id="+IDAO.getId()+","
                + "estado='"+IDAO.getEstado()+"',"
                + "descripcion='"+IDAO.getDescripcion()+"'"
                + "where persona_identificacion="+IDAO.getPersonaIdentificacion();
        modificar=model.getCon().prepareStatement(sql);
        modificar.executeUpdate();
    }

    public void eliminar(Rol IDAO) throws SQLException {
        model= Model.getInstance();
        
        String sql="delete from Rol where persona_identificacion="+IDAO.getPersonaIdentificacion();
        eliminar=model.getCon().prepareStatement(sql);
        eliminar.executeUpdate();
    }

    public LinkedList<Rol> listar() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
